package service

import (
	srccreate "ServiceEdge/controllers/src/create"
	srcinfo "ServiceEdge/controllers/src/srcInfo"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"net/http"
)

type CreatePodInfo struct {
	PodInfo          *srcinfo.PodInfo          `json:"podInfo"`
	NodeSelectorInfo *srcinfo.NodeSelectorInfo `json:"nodeSelectorInfo"`
}

// 创建pod到指定node，使用nodeselector
func CreatePod(c *gin.Context) {
	var jsonData CreatePodInfo

	// 解析JSON参数
	if err := c.ShouldBindJSON(&jsonData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	logrus.Infof("Received JSON: %+v\n", jsonData)
	// 检查 podInfo 和 nodeSelectorInfo 是否为 nil
	if jsonData.PodInfo == nil || jsonData.NodeSelectorInfo == nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "podInfo and nodeSelectorInfo are required"})
		logrus.Infof("podInfo and nodeSelectorInfo are required")
		return
	}

	podInfo, nodeSelectorInfo := jsonData.PodInfo, jsonData.NodeSelectorInfo

	err := srccreate.CteatePodByNodeLable(podInfo, nodeSelectorInfo)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"status": "fail"})
		return
	}
	// 返回成功响应
	c.JSON(http.StatusOK, gin.H{"status": "success"})
}
